<template>
  <div class="dashboard-container">
    <div class="app-container">
      <div v-loading="loading" class="edit-form">
        <el-form label-width="auto">
          <el-row>
            <el-col :span="12">
              <el-form-item label="姓名">
                <el-input :value="userInfo.username" size="mini" class="inputW" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="角色">
                <el-checkbox-group v-model="roleIds" class="inputW">
                  <el-checkbox v-for="(item,index) in enableList" :key="index.id" :label="item.id" style="width: 380px;">
                    {{ item.name }}
                  </el-checkbox>
                </el-checkbox-group>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row type="flex">
            <el-col>
              <el-form-item>
                <el-button type="primary" size="mini" @click="saveUpload">保存更新</el-button>
                <el-button size="mini" @click="$router.replace('/employee')">返回</el-button>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import { getEnableList } from '@/api/role.js'
import { getEmployeeInfo, uploadEmployee_Role } from '@/api/employee.js'
export default {
  name: 'AssignRole',
  data() {
    return {
      loading: false,
      enableList: [], // 已启用的角色列表
      userInfo: {}, // 员工基本信息
      roleIds: [] // 得到此员工担任的角色
    }
  },
  created() {
    this.GetEmployeeInfo(this.$route.params.id)
  },
  methods: {
    // 获取启用的角色列表
    async GetEnableList() {
      this.enableList = await getEnableList()
      console.log(this.enableList)

      this.loading = false
    },
    // 获取员工基本信息
    async GetEmployeeInfo(id) {
      this.loading = true
      this.userInfo = await getEmployeeInfo(id)
      this.roleIds = this.userInfo.roleIds
      this.GetEnableList() // 获取启用角色列表
    },
    // 保存更新
    async saveUpload() {
      await uploadEmployee_Role(this.$route.params.id, this.roleIds)
      this.$router.replace('/employee')
      this.$message.success('角色分配成功')
    }
  }
}
</script>

<style scoped lang="scss">
.edit-form {
display: flex;
align-items: center;
justify-content: center;
  background: #fff;
  padding: 20px;
  .inputW {
    width: 380px
  }
}
</style>

